home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / mac / raytrace / dkb / mcprt102.bnh / DKBTrace-Mac / mac_mpw.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-10  |  20.2 KB  |  906 lines

  1. #include "frame.h"
  2. #include "dkbproto.h"
  3.  
  4. #include <stdlib.h>
  5. #include <setjmp.h>
  6. #include <Controls.h>
  7. #include <Desk.h>
  8. #include <Dialogs.h>
  9. #include <Events.h>
  10. #include <Files.h>
  11. #include <Fonts.h>
  12. #include <Memory.h>
  13. #include <Menus.h>
  14. #include <Notification.h>
  15. #include <OSEvents.h>
  16. #include <OSUtils.h>
  17. #include <Packages.h>
  18. #include <QuickDraw.h>
  19. #include <QuickDraw32Bit.h>
  20. #include <Resources.h>
  21. #include <SegLoad.h>
  22. #include <TextEdit.h>
  23. #include <Types.h>
  24. #include <Windows.h>
  25. #include <CursorCtl.h>
  26.  
  27. #define    maxlines        300
  28. #define    wheight            25
  29. #define    wwidth            80
  30. #define    margin            3
  31.  
  32. #define    MYBUFSIZE        256
  33. #define    N_ARGS            32
  34.  
  35. void MacDawdle(void);
  36.  
  37. typedef struct
  38. {
  39.     char    paths[256];
  40.     char    filename[32];
  41.     short    vRefNum;
  42.     short    width, height, from, to;
  43.     float    threshold;
  44.     short    bufsize, quality;
  45.     char    cr_file, dump, raw, targa;
  46.     char    antialias, use_buf, resume, pause;
  47.     char    verbose, debug;
  48.     /* Options Menu states */
  49.     char    dither;
  50. } set_rec;
  51.  
  52. typedef unsigned char TEXTBUF[maxlines][wwidth + 1];
  53. typedef TEXTBUF *TEXTPTR;
  54.  
  55. volatile extern int Stop_Flag;
  56. extern unsigned int Options;
  57. jmp_buf        env;
  58. int            dkb_runs = 0;    /* Used to determine the menu states. */
  59.  
  60. PixMapHandle pm;
  61. WindowPtr    plot_window = NULL;
  62. Ptr            plot_pixmap = NULL;
  63. Rect        plot_bounds;
  64.  
  65. FILE        *my_out, *my_err;
  66. WindowPtr    myWindow[2];
  67. MenuHandle    myMenus[5];
  68. RgnHandle    theUpdateRgn;
  69. ControlHandle myScroll[2];
  70. char        run_flag = 0, quit_flag = 0, waiting_for_esc = 0;
  71. EventRecord    myEvent;
  72. int            nlines[2] = {0, 0}, topline[2] = {0, 0}, active[2] = {0, 0};
  73. int            cur_wnd, ARGC;
  74. TEXTPTR        myText;
  75. Rect        tRect = {0, 0, 11 * wheight + 2 * margin - 1, 6 * wwidth + 2 * margin - 1};
  76. long        prev_upd_cons, prev_upd_plot;
  77. char        buf0[MYBUFSIZE], buf1[MYBUFSIZE], *ARGV[32], argstr[1024], *argptr;
  78. set_rec        *settings, **settings_h;
  79.  
  80. /* --- These are the required routines for machine.h --- */
  81.  
  82. void display_init(int width, int height)
  83. {
  84.     long    *p, *q, pmsize;
  85.     Handle    h;
  86.     Rect    brect;
  87.     
  88.     SetRect(&plot_bounds, 0, 0, width, height);
  89.     pmsize = 4 * width * height;
  90.     h = NewHandle(pmsize);
  91.     if (h)
  92.     {
  93.         MoveHHi(h);
  94.         HLock(h);
  95.         plot_pixmap = *h;
  96.         q = (long *) ((long) plot_pixmap + pmsize);
  97.         for (p = (long *) plot_pixmap; p < q; *p++ = -1);
  98.         (*pm)->bounds = plot_bounds;
  99.         (*pm)->rowBytes = width * 4 | 0x8000;
  100.         (*pm)->baseAddr = plot_pixmap;
  101.     }
  102.     else
  103.         plot_pixmap = NULL;
  104.     
  105.     brect = plot_bounds;
  106.     OffsetRect(&brect, 8, 45);
  107.     plot_window = NewCWindow(NULL, &brect, "\pPlot", true, noGrowDocProc,
  108.                                                         (WindowPtr) -1L, false, 0);
  109. }
  110.  
  111. void display_plot(int x, int y, char Red, char Green, char Blue)
  112. {
  113.     RGBColor c;
  114.     char *pptr;
  115.     
  116.     SetPort(plot_window);
  117.     c.red = ((short) Red) << 8;
  118.     c.green = ((short) Green) << 8;
  119.     c.blue = ((short) Blue) << 8;
  120.     SetCPixel(x, y, &c);
  121.  
  122.     if (plot_pixmap)
  123.     {
  124.         pptr = (char *) ((long) plot_pixmap + 4 * (plot_bounds.right * y + x));
  125.         *pptr++ = 0;
  126.         *pptr++ = Red;
  127.         *pptr++ = Green;
  128.         *pptr = Blue;
  129.     }
  130. }
  131.  
  132. void display_close(void)
  133. {
  134.     Handle h;
  135.     NMRec myNote;
  136.     
  137.     if ((Options & PROMPTEXIT) && !quit_flag)
  138.     {
  139.         fprintf(stderr, "### Press [Esc] to continue...\n");
  140.         SetWTitle(plot_window, "\pPress [Esc]");
  141.         SelectWindow(plot_window);
  142.         myNote.qType = nmType;
  143.         myNote.nmFlags = 0;
  144.         myNote.nmPrivate = 0;
  145.         myNote.nmReserved = 0;
  146.         myNote.nmMark = 0;
  147.         myNote.nmSIcon = GetResource('SICN', 128);
  148.         myNote.nmSound = (Handle) -1;
  149.         myNote.nmStr = NULL;
  150.         myNote.nmResp = NULL;
  151.         NMInstall((QElemPtr) &myNote);
  152.         DisableItem(myMenus[2], 0);
  153.         DrawMenuBar();
  154.         
  155.         waiting_for_esc = 1;
  156.         while (waiting_for_esc)
  157.             MacDawdle();
  158.         
  159.         NMRemove((QElemPtr) &myNote);
  160.         EnableItem(myMenus[2], 0);
  161.         DrawMenuBar();
  162.     }
  163.     DisposeWindow(plot_window);
  164.     plot_window = NULL;
  165.     if (plot_pixmap)
  166.     {
  167.         h = RecoverHandle(plot_pixmap);
  168.         HUnlock(h);
  169.         DisposHandle(h);
  170.         plot_pixmap = NULL;
  171.     }
  172. }
  173.  
  174. void display_finished(void)
  175. {
  176. }
  177.  
  178. /* --- And now for the real Mac stuff --- */
  179.  
  180. char *PrintTime()
  181. {
  182.     static char        t[10];
  183.     unsigned long    secs;
  184.     
  185.     GetDateTime(&secs);
  186.     iutimestring(secs, true, t);
  187.     return t;
  188. }
  189.  
  190. void call_main(int argc, char *argv[])
  191. {
  192.     if (!setjmp(env))
  193.     {
  194.         fprintf(stderr, "### %s Starting...\n\n", PrintTime());
  195.         dkb_runs = 1;
  196.         Main(argc, argv);
  197.         dkb_runs = 0;
  198.         fprintf(stderr, "### %s Normal termination.\n\n", PrintTime());
  199.     }
  200.     run_flag = 0;
  201. }
  202.  
  203. void exit(int status)
  204. {
  205.     dkb_runs = 0;
  206.     fprintf(stderr, "### %s Terminated by exit(%d).\n\n", PrintTime(), status);
  207.     longjmp(env, 1);
  208. }
  209.  
  210. char *getenv(const char *name)
  211. {
  212.     if (!strcmp(name, "DKBOPT"))
  213.         return "";
  214.     else
  215.         return NULL;
  216. }
  217.  
  218. void SetTypeAndCreator(char *name, long type, long creator)
  219. {
  220.     FInfo finf;
  221.     
  222.     getfinfo(name, 0, &finf);
  223.     finf.fdType = (OSType) type;
  224.     finf.fdCreator = (OSType) creator;
  225.     setfinfo(name, 0, &finf);
  226. }
  227.  
  228. void UpdateLine(int n)
  229. {
  230.     int L;
  231.     
  232.     if ((L = n + topline[cur_wnd]) < nlines[cur_wnd])
  233.     {
  234.         SetPort(myWindow[cur_wnd]);
  235.         MoveTo(margin, 11 * n + 8 + margin);
  236.         drawstring(myText[cur_wnd][L]);
  237.     }
  238. }
  239.     
  240. void UpdateText(void)
  241. {
  242.     int i;
  243.  
  244.     for (i = 0; i < wheight; i++)
  245.         UpdateLine(i);
  246. }
  247.  
  248. void ScrollBits(void)
  249. {
  250.     int            prevTopLine, offset;
  251.  
  252.     prevTopLine = topline[cur_wnd];
  253.     topline[cur_wnd] = GetCtlValue(myScroll[cur_wnd]);
  254.     offset = prevTopLine - topline[cur_wnd];
  255.     ScrollRect(&tRect, 0, offset * 11, theUpdateRgn);
  256.     ClipRect(&(*theUpdateRgn)->rgnBBox);
  257.     UpdateText();
  258.     ClipRect(&myWindow[cur_wnd]->portRect);
  259. }
  260.     
  261. pascal void ScrollUp(ControlHandle whichControl, short part)
  262. {
  263.     if (part == inUpButton)
  264.     {
  265.         SetCtlValue(whichControl, GetCtlValue(whichControl) - 1);
  266.         ScrollBits();
  267.     }
  268. }
  269.     
  270. pascal void ScrollDown(ControlHandle whichControl, short part)
  271. {
  272.     if (part == inDownButton)
  273.     {
  274.         SetCtlValue(whichControl, GetCtlValue(whichControl) + 1);
  275.         ScrollBits();
  276.     }
  277. }
  278.     
  279. void PageScroll(short code, short amount)
  280. {
  281.     Point pt;
  282.  
  283.     do
  284.     {
  285.         GetMouse(&pt);
  286.         if (TestControl(myScroll[cur_wnd], pt) == code)
  287.         {
  288.             SetCtlValue(myScroll[cur_wnd], GetCtlValue(myScroll[cur_wnd]) + amount);
  289.             ScrollBits();
  290.         }
  291.     }
  292.     while (StillDown());
  293. }
  294.  
  295. void PrintLine(char s[])
  296. {
  297.     short max;
  298.     
  299.     SetPort(myWindow[cur_wnd]);
  300.     if (nlines[cur_wnd] == maxlines)
  301.     {
  302.         BlockMove(&myText[cur_wnd][1], &myText[cur_wnd][0], ((long) wwidth + 1) * (maxlines - 1));
  303.         strncpy(myText[cur_wnd][nlines[cur_wnd] - 1], s, wwidth);
  304.         SetCtlValue(myScroll[cur_wnd], nlines[cur_wnd] - wheight);
  305.         topline[cur_wnd]--;
  306.         ScrollBits();
  307.         UpdateLine(wheight - 1);
  308.     }
  309.     else
  310.     {
  311.         strncpy(myText[cur_wnd][nlines[cur_wnd]], s, wwidth);
  312.         myText[cur_wnd][nlines[cur_wnd]][wwidth] = 0;
  313.         if (nlines[cur_wnd] < wheight)
  314.             UpdateLine(nlines[cur_wnd]++);
  315.         else
  316.         {
  317.             SetCtlMax(myScroll[cur_wnd], max = ++nlines[cur_wnd] - wheight);
  318.             SetCtlValue(myScroll[cur_wnd], max);
  319.             ScrollBits();
  320.             UpdateLine(wheight - 1);
  321.         }
  322.     }
  323. }
  324.  
  325. void AboutDKB(void)
  326. {
  327.     char *s = "DKBTrace";
  328.     
  329.     Stop_Flag = 0;
  330.     call_main(1, &s);
  331. }
  332.  
  333. void AddArg(const char *s)
  334. {
  335.     ARGV[ARGC++] = argptr;
  336.     while (*argptr++ = *s++);
  337. }
  338.  
  339. pascal Boolean MyFilter(DialogPtr theDialog, EventRecord *theEvent, short *itemHit)
  340. {
  341.     return false;
  342. }
  343.  
  344. short PinValue(short *x, short a, short b)
  345. {
  346.     if (*x < a)
  347.         *x = a;
  348.     else
  349.         if (*x > b)
  350.             *x = b;
  351. }
  352.  
  353. void StartDKB(void)
  354. {
  355.     DialogPtr    myDialog;
  356.     short        i, itemHit, dummyInt, temp_vRefNum;
  357.     Handle        dummyHand;
  358.     Rect        displayRect;
  359.     ControlHandle cntl[23];
  360.     char        s1[256], s2[256], *s2_p, *s3_p, c;
  361.     SFReply        reply;
  362.     SFTypeList    typelist;
  363.     Point        where;
  364.     
  365.     myDialog = GetNewDialog(128, NULL, (WindowPtr) -1);
  366.     for (i = 1; i < 23; i++)
  367.         GetDItem(myDialog, i, &dummyInt, (Handle *) &cntl[i], &displayRect);
  368.     setitext((Handle) cntl[4], settings->filename);
  369.     sprintf(s1, "%d", settings->width); setitext((Handle) cntl[5], s1);
  370.     sprintf(s1, "%d", settings->height); setitext((Handle) cntl[6], s1);
  371.     sprintf(s1, "%d", settings->quality); setitext((Handle) cntl[7], s1);
  372.     sprintf(s1, "%d", settings->from); setitext((Handle) cntl[8], s1);
  373.     sprintf(s1, "%d", settings->to); setitext((Handle) cntl[9], s1);
  374.     SetCtlValue(cntl[10], settings->antialias);
  375.     sprintf(s1, "%.2f", settings->threshold); setitext((Handle) cntl[11], s1);
  376.     SetCtlValue(cntl[12], settings->use_buf);
  377.     sprintf(s1, "%d", settings->bufsize); setitext((Handle) cntl[13], s1);
  378.     SetCtlValue(cntl[14], settings->cr_file);
  379.     SetCtlValue(cntl[15], settings->dump);
  380.     SetCtlValue(cntl[16], settings->raw);
  381.     SetCtlValue(cntl[17], settings->targa);
  382.     SetCtlValue(cntl[18], settings->resume = 0);
  383.     SetCtlValue(cntl[19], settings->pause);
  384.     SetCtlValue(cntl[20], settings->verbose);
  385.     SetCtlValue(cntl[21], settings->debug);
  386.     setitext((Handle) cntl[22], settings->paths);
  387.     SelIText(myDialog, 5, 0, 32767);
  388.     if (!(*settings->filename))
  389.         HiliteControl(cntl[1], 255);
  390.     
  391.     do
  392.     {
  393.         ModalDialog(MyFilter, &itemHit);
  394.         switch (itemHit)
  395.         {
  396.             case 3:
  397.                 where.h = where.v = 82;
  398.                 typelist[0] = 'TEXT';
  399.                 sfgetfile(&where, "Select data file╔", NULL, 1, typelist, NULL, &reply); 
  400.                 if (reply.good)
  401.                 {
  402.                     SetIText((Handle) cntl[4], reply.fName);
  403.                     temp_vRefNum = reply.vRefNum;
  404.                     HiliteControl(cntl[1], 0);
  405.                 }
  406.                 break;
  407.             case 10: case 12: case 14:
  408.             case 18: case 19: case 20: case 21:
  409.                 SetCtlValue(cntl[itemHit], !GetCtlValue(cntl[itemHit]));
  410.                 break;
  411.             case 15: case 16: case 17:
  412.                 for (i = 15; i <= 17; i++)
  413.                     SetCtlValue(cntl[i], itemHit == i);
  414.                 break;
  415.         }
  416.     }
  417.     while (itemHit != 1 && itemHit != 2);
  418.     
  419.     if (itemHit == 1)
  420.     {
  421.         getitext((Handle) cntl[4], settings->filename);
  422.         settings->vRefNum = temp_vRefNum;
  423.         getitext((Handle) cntl[5], s1); sscanf(s1, "%hd", &settings->width);
  424.         PinValue(&settings->width, 0, 4095);
  425.         getitext((Handle) cntl[6], s1); sscanf(s1, "%hd", &settings->height);
  426.         PinValue(&settings->height, 0, 4095);
  427.         getitext((Handle) cntl[7], s1); sscanf(s1, "%hd", &settings->quality);
  428.         getitext((Handle) cntl[8], s1); sscanf(s1, "%hd", &settings->from);
  429.         PinValue(&settings->from, 0, settings->height);
  430.         getitext((Handle) cntl[9], s1); sscanf(s1, "%hd", &settings->to);
  431.         PinValue(&settings->to, settings->from, settings->height);
  432.         settings->antialias = GetCtlValue(cntl[10]);
  433.         getitext((Handle) cntl[11], s1); sscanf(s1, "%f", &settings->threshold);
  434.         settings->use_buf = GetCtlValue(cntl[12]);
  435.         getitext((Handle) cntl[13], s1); sscanf(s1, "%hd", &settings->bufsize);
  436.         settings->cr_file = GetCtlValue(cntl[14]);
  437.         settings->dump = GetCtlValue(cntl[15]);
  438.         settings->raw = GetCtlValue(cntl[16]);
  439.         settings->targa = GetCtlValue(cntl[17]);
  440.         settings->resume = GetCtlValue(cntl[18]);
  441.         settings->pause = GetCtlValue(cntl[19]);
  442.         settings->verbose = GetCtlValue(cntl[20]);
  443.         settings->debug = GetCtlValue(cntl[21]);
  444.         getitext((Handle) cntl[22], settings->paths);
  445.         
  446.         if (*settings->filename)
  447.         {
  448.             ARGC = 0;
  449.             argptr = argstr;
  450.             AddArg("DKBTrace");
  451.             AddArg("+d0");
  452.             AddArg("-x");
  453.             sprintf(s1, "-w%d", settings->width); AddArg(s1);
  454.             sprintf(s1, "-h%d", settings->height); AddArg(s1);
  455.             sprintf(s1, "-s%d", settings->from); AddArg(s1);
  456.             sprintf(s1, "-e%d", settings->to); AddArg(s1);
  457.             if (settings->antialias)
  458.             {
  459.                 sprintf(s1, "+a%.2f", settings->threshold);
  460.                 AddArg(s1);
  461.             }
  462.             else
  463.                 AddArg("-a");
  464.             if (settings->use_buf)
  465.             {
  466.                 sprintf(s1, "-b%d", settings->bufsize);
  467.                 AddArg(s1);
  468.             }
  469.             sprintf(s1, "-q%d", settings->quality); AddArg(s1);
  470.             AddArg(settings->cr_file ? settings->dump ? "+fd" : settings->raw ?
  471.                                                                 "+fr" : "+ft" : "-f");
  472.             AddArg(settings->resume ? "+c" : "-c");
  473.             AddArg(settings->pause ? "+p" : "-p");
  474.             AddArg(settings->verbose ? "+v" : "-v");
  475.             AddArg(settings->debug ? "+z" : "-z");
  476.             sprintf(s1, "-i%s", settings->filename); AddArg(s1);
  477.             
  478.             /* Strip .dat or .data off end of inputfilename. */
  479.             strcpy(s2, settings->filename);
  480.             if ((i = strlen(s2)) > 4)
  481.             {
  482.                 s2_p = s2 + i - 4;
  483.                 if (equalstring(s2_p, ".dat", false, true)
  484.                                 || i > 5 && equalstring(--s2_p, ".data", false, true))
  485.                     *s2_p = 0;
  486.             }
  487.             /* Append appropriate extension. For Raw files, DKB takes care of this. */
  488.             if (settings->dump)
  489.                 strcat(s2, ".dis");
  490.             else
  491.                 if (settings->targa)
  492.                     strcat(s2, ".tga");
  493.             sprintf(s1, "-o%s", s2); AddArg(s1);
  494.             
  495.             /* Add -l options for library paths. */
  496.             strcpy(s2, settings->paths);
  497.             c = *s2;
  498.             s3_p = s2_p = s2;
  499.             while (c)
  500.             {
  501.                 while (*s3_p && *s3_p != 13)
  502.                     s3_p++;
  503.                 c = *s3_p;
  504.                 *s3_p = 0;
  505.                 /* Remove trailing ':'; DKB takes care of appending that. */
  506.                 if (*(s3_p - 1) == ':')
  507.                     *(s3_p - 1) = 0;
  508.                 if (*s2_p)
  509.                 {
  510.                     sprintf(s1, "-l%s", s2_p);
  511.                     AddArg(s1);
  512.                 }
  513.                 s2_p = ++s3_p;
  514.             }
  515.             
  516.             Stop_Flag = 0;
  517.             run_flag = 1;
  518.             SetVol(NULL, settings->vRefNum);
  519.         }
  520.     }
  521.     DisposDialog(myDialog);
  522. }
  523.  
  524. void UpdateMenus(void)
  525. {
  526.     if (dkb_runs)
  527.     {
  528.         DisableItem(myMenus[1], 1);
  529.         DisableItem(myMenus[2], 1);
  530.         EnableItem(myMenus[2], 2);
  531.     }
  532.     else
  533.     {
  534.         EnableItem(myMenus[1], 1);
  535.         EnableItem(myMenus[2], 1);
  536.         DisableItem(myMenus[2], 2);
  537.     }
  538.     CheckItem(myMenus[4], 1, settings->dither);
  539. }
  540.  
  541. void DoCommand(long m)
  542. {
  543.     short    theMenu, theItem;
  544.     Str255    name;
  545.     
  546.     theMenu = m >> 16;
  547.     theItem = m;
  548.     switch (theMenu)
  549.     {
  550.         case 1:
  551.             if (theItem == 1)
  552.                 AboutDKB();
  553.             else
  554.             {
  555.                 GetItem(myMenus[1], theItem, name);
  556.                 OpenDeskAcc(name);
  557.             }
  558.             break;
  559.             
  560.         case 2:
  561.             switch (theItem)
  562.             {
  563.                 case 1: if (!run_flag) StartDKB(); break;
  564.                 case 2: Stop_Flag = 1; break;
  565.                 case 4:    Stop_Flag = quit_flag = 1;
  566.             }
  567.             break;
  568.             
  569.         case 3:
  570.             SystemEdit(theItem - 1);
  571.             break;
  572.         
  573.         case 4:
  574.             settings->dither = !settings->dither;
  575.             if (plot_window)
  576.             {
  577.                 SetPort(plot_window);
  578.                 InvalRect(&plot_window->portRect);
  579.             }
  580.     }
  581.     HiliteMenu(0);
  582. }
  583.  
  584. void DoMouseDown(void)
  585. {
  586.     short            code, part;
  587.     WindowPtr        whichWindow;
  588.     ControlHandle    whichControl;
  589.  
  590.     code = FindWindow(myEvent.where, &whichWindow);
  591.     switch (code)
  592.     {
  593.         case inMenuBar:
  594.             UpdateMenus();
  595.             DoCommand(MenuSelect(myEvent.where));
  596.             break;
  597.         case inSysWindow:
  598.             SystemClick(&myEvent, whichWindow);
  599.             break;
  600.         case inDrag:
  601.             DragWindow(whichWindow, myEvent.where, &qd.screenBits.bounds);
  602.             break;
  603.         case inContent:
  604.             if (whichWindow != FrontWindow())
  605.                 SelectWindow(whichWindow);
  606.             else
  607.             {
  608.                 SetPort(whichWindow);
  609.                 GlobalToLocal(&myEvent.where);
  610.                 part = FindControl(myEvent.where, whichWindow, &whichControl);
  611.                 if (whichControl == NULL) break;
  612.                 cur_wnd = whichControl != myScroll[0];
  613.                 switch (part)
  614.                 {
  615.                     case inUpButton:
  616.                         TrackControl(whichControl, myEvent.where, (ProcPtr) ScrollUp);
  617.                         break;
  618.                     case inDownButton:
  619.                         TrackControl(whichControl, myEvent.where, (ProcPtr) ScrollDown);
  620.                         break;
  621.                     case inPageUp:
  622.                         PageScroll(part, -wheight + 1);
  623.                         break;
  624.                     case inPageDown:
  625.                         PageScroll(part, wheight - 1);
  626.                         break;
  627.                     case inThumb:
  628.                     {
  629.                         TrackControl(whichControl, myEvent.where, NULL);
  630.                         ScrollBits();
  631.                     }
  632.                 }
  633.             }
  634.     }
  635. }
  636.     
  637. void DoKeyDown(void)
  638. {
  639.     char    theChar;
  640.  
  641.     theChar = myEvent.message & charCodeMask;
  642.     if (theChar == 27)
  643.         waiting_for_esc = 0;
  644.     else if (theChar == 5 && !dkb_runs)
  645.     {
  646.         HiliteMenu(1);
  647.         AboutDKB();
  648.         HiliteMenu(0);
  649.     }
  650.     else if (myEvent.modifiers & cmdKey)
  651.     {
  652.         UpdateMenus();
  653.         DoCommand(MenuKey(theChar));
  654.     }
  655. }
  656.  
  657. void DoUpdateEvt(void)
  658. {
  659.     WindowPtr    wnd;
  660.     BitMap        bmap;
  661.     
  662.     wnd = (WindowPtr) myEvent.message;
  663.     BeginUpdate(wnd);
  664.     SetPort(wnd);
  665.     if (wnd == plot_window)
  666.     {
  667.         if (plot_pixmap)
  668.         {
  669.             bmap.baseAddr = (Ptr) pm;
  670.             bmap.rowBytes = 0xC000;
  671.             CopyBits(&bmap, &wnd->portBits, &plot_bounds, &plot_bounds,
  672.                                 settings->dither ? ditherCopy : srcCopy, NULL);
  673.         }
  674.      }
  675.     else
  676.     {
  677.         cur_wnd = wnd != myWindow[0];
  678.         TextFont(4);
  679.         TextSize(9);
  680.         UpdateText();
  681.         if (active[cur_wnd])
  682.             DrawControls(myWindow[cur_wnd]);
  683.         else
  684.             FrameRect(&(*myScroll[cur_wnd])->contrlRect);
  685.     }
  686.     EndUpdate(wnd);
  687. }
  688.  
  689. void Activate(WindowPtr theWindow)
  690. {
  691.     if ((cur_wnd = (theWindow == myWindow[1])) || theWindow == myWindow[0])
  692.     {
  693.         SetPort(myWindow[cur_wnd]);
  694.         ShowControl(myScroll[cur_wnd]);
  695.         active[cur_wnd] = 1;
  696.     }
  697. }
  698.  
  699. void Deactivate(WindowPtr theWindow)
  700. {
  701.     if ((cur_wnd = (theWindow == myWindow[1])) || theWindow == myWindow[0])
  702.     {
  703.         SetPort(myWindow[cur_wnd]);
  704.         HideControl(myScroll[cur_wnd]);
  705.         FrameRect(&(*myScroll[cur_wnd])->contrlRect);
  706.         active[cur_wnd] = 0;
  707.     }
  708. }
  709.  
  710. void DoActivateEvt(void)
  711. {
  712.     if (myEvent.modifiers & 1)
  713.         Activate((WindowPtr) myEvent.message);
  714.     else
  715.         Deactivate((WindowPtr) myEvent.message);
  716. }
  717.  
  718. void DoApp4Evt(void)
  719. {
  720.     if (myEvent.message >> 24 == 1)
  721.     {
  722.         if (myEvent.message & 1)
  723.             Activate(FrontWindow());
  724.         else
  725.             Deactivate(FrontWindow());
  726.         SetCursor(&qd.arrow);
  727.     }
  728. }
  729.  
  730. void DoNullEvt(void)
  731. {
  732.     char    s[256];
  733.     long    pos;
  734.     
  735.     if (myEvent.when - prev_upd_cons > 60)
  736.     {
  737.         cur_wnd = 0;
  738.         for (;;)
  739.         {
  740.             pos = ftell(my_out);
  741.             fseek(my_out, pos, SEEK_SET);
  742.             if (fgets(s, 256, my_out) == NULL) break;
  743.             PrintLine(s);
  744.         }
  745.         cur_wnd = 1;
  746.         for (;;)
  747.         {
  748.             pos = ftell(my_err);
  749.             fseek(my_err, pos, SEEK_SET);
  750.             if (fgets(s, 256, my_err) == NULL) break;
  751.             PrintLine(s);
  752.         }
  753.         prev_upd_cons = myEvent.when;
  754.     }
  755.     if (plot_window && plot_pixmap && settings->dither
  756.                                     && myEvent.when - prev_upd_plot > 3600)
  757.     {
  758.         SetPort(plot_window);
  759.         InvalRect(&plot_window->portRect);
  760.         prev_upd_plot = myEvent.when;
  761.     }
  762. }
  763.  
  764. void MacDawdle(void)
  765. {
  766.     WaitNextEvent(everyEvent, &myEvent, 0, NULL);
  767.     switch (myEvent.what)
  768.     {
  769.         case mouseDown:        DoMouseDown(); break;
  770.         case keyDown:        DoKeyDown(); break;
  771.         case updateEvt:        DoUpdateEvt(); break;
  772.         case activateEvt:    DoActivateEvt(); break;
  773.         case app4Evt:        DoApp4Evt(); break;
  774.         case nullEvent:        DoNullEvt();
  775.     }
  776. }
  777.  
  778. void MacBrag(void)
  779. {
  780.     printf("  Ported to MPW, and Mac front-end added, by Thomas Okken.\n\n");
  781. }
  782.  
  783. main()
  784. {
  785.     long        stk_size;
  786.     int            i;
  787.     Rect        r;
  788.     Handle        h;
  789.     CTabHandle    ctH;
  790.     SysEnvRec    theWorld;
  791.     
  792.     /* Allocate more memory for stack. */
  793.     h = Get1Resource('CNFG', 128);
  794.     stk_size = ** ((long **) h);
  795.     ReleaseResource(h);
  796.     SetApplLimit(GetApplLimit() - stk_size);
  797.     MaxApplZone();
  798.     
  799.     /* Initialize toolbox managers. */
  800.     InitGraf(&qd.thePort);
  801.     InitFonts();
  802.     InitWindows();
  803.     InitMenus();
  804.     TEInit();
  805.     InitDialogs(NULL);
  806.     InitCursor();
  807.     SetCursor(&qd.arrow);
  808.     FlushEvents(everyEvent, 0);
  809.     for (i = 1; i <= 3; i++)
  810.         GetNextEvent(everyEvent, &myEvent);
  811.     prev_upd_cons = prev_upd_plot = myEvent.when;
  812.     
  813.     /* Check if the necessary hard- & software is present. */
  814.     if (SysEnvirons(1, &theWorld) != noErr || theWorld.processor < env68020
  815.         || !theWorld.hasFPU || theWorld.systemVersion < 0x0605 || !theWorld.hasColorQD
  816.         || NGetTrapAddress(0xAB03, ToolTrap) == NGetTrapAddress(0xA89F, ToolTrap))
  817.     {
  818.         StopAlert(129, NULL);
  819.         ExitToShell();
  820.     }
  821.     
  822.     /* Set up menus. */
  823.     for (i = 1; i <= 4; i++)
  824.         myMenus[i] = GetMenu(i + 127);
  825.     AddResMenu(myMenus[1], 'DRVR');
  826.     for (i = 1; i <= 4; i++)
  827.         InsertMenu(myMenus[i], 0);
  828.     DrawMenuBar();
  829.  
  830.     /* Redirect stdout and stderr to files; open those for reading. */
  831.     freopen("DKB.out", "w", stdout);
  832.     freopen("DKB.err", "w", stderr);
  833.     SetTypeAndCreator("DKB.out", 'TEXT', 'MPS ');
  834.     SetTypeAndCreator("DKB.err", 'TEXT', 'MPS ');
  835.     setvbuf(stdout, buf0, _IOLBF, MYBUFSIZE);
  836.     setvbuf(stderr, buf1, _IOLBF, MYBUFSIZE);
  837.     my_out = fopen("DKB.out", "r");
  838.     my_err = fopen("DKB.err", "r");
  839.     
  840.     /* Initialize console windows. */
  841.     myText = (TEXTPTR) NewPtr(2 * maxlines * (wwidth + 1));
  842.     r = tRect;
  843.     OffsetRect(&r, 3, 59);
  844.     r.right += 16;
  845.     myWindow[1] = NewWindow(NULL, &r, "\pError", true, noGrowDocProc, myWindow[0], false, 0);
  846.     OffsetRect(&r, 6, -19);
  847.     myWindow[0] = NewWindow(NULL, &r, "\pOutput", true, noGrowDocProc, (WindowPtr) -1, false, 0);
  848.     r = tRect;
  849.     InsetRect(&r, -1, -1);
  850.     r.left = r.right;
  851.     r.right += 16;
  852.     myScroll[0] = NewControl(myWindow[0], &r, "", true, 0, 0, 0, scrollBarProc, 0);
  853.     myScroll[1] = NewControl(myWindow[1], &r, "", true, 0, 0, 0, scrollBarProc, 0);
  854.     InsetRect(&tRect, margin, margin);
  855.  
  856.     /* Get saved settings for Trace╔ dialog. */
  857.     theUpdateRgn = NewRgn();
  858.     settings_h = (set_rec **) GetResource('SETT', 128);
  859.     MoveHHi((Handle) settings_h);
  860.     HLock((Handle) settings_h);
  861.     settings = *settings_h;
  862.     *(settings->filename) = 0;
  863.     
  864.     /* Set up offscreen pixmap. */
  865.     ctH = (CTabHandle) NewHandle(sizeof(ColorTable));
  866.     (*ctH)->ctSeed = 24;
  867.     (*ctH)->ctFlags = 0;
  868.     (*ctH)->ctSize = 0;
  869.     pm = (PixMapHandle) NewHandle(sizeof(PixMap));
  870.     (*pm)->pmVersion = 0;
  871.     (*pm)->packType = 0;
  872.     (*pm)->packSize = 0;
  873.     (*pm)->hRes = 0x480000;
  874.     (*pm)->vRes = 0x480000;
  875.     (*pm)->pixelType = RGBDirect;
  876.     (*pm)->pixelSize = 32;
  877.     (*pm)->cmpCount = 3;
  878.     (*pm)->cmpSize = 8;
  879.     (*pm)->planeBytes = 0;
  880.     (*pm)->pmTable = ctH;
  881.     (*pm)->pmReserved = 0;
  882.     
  883.     /* Main event loop, sort of. */
  884.     while (!quit_flag)
  885.     {
  886.         MacDawdle();
  887.         if (run_flag)
  888.         {
  889.             call_main(ARGC, ARGV);
  890.             SysBeep(10);
  891.         }
  892.         else
  893.             if (quit_flag) break;
  894.     }
  895.     
  896.     /* Clean up & exit. */
  897.     DisposeWindow(myWindow[0]);
  898.     DisposeWindow(myWindow[1]);
  899.     DisposPtr((Ptr) myText);
  900.     fclose(my_out);
  901.     fclose(my_err);
  902.     HUnlock((Handle) settings_h);
  903.     ChangedResource((Handle) settings_h);
  904.     ExitToShell();
  905. }
  906.